//
//  LoginViewModel.swift
//  SwiftUI+MVVM+Repository
//
//  Created by 高强 on 2024/9/10.
//

import Foundation

class LoginViewModel: ObservableObject {
    
    @Published var name: String = "admin"
    @Published var password: String = "123456"
    
    @Published var showError: Bool = false
    @Published var errorMessage: String = ""
    @Published var isLoginSuccessful = false
    
    private var repository = LoginRepository()
    
    
    func login(){
        repository.login(username: name, password: password){[weak self] success,error in
            DispatchQueue.main.async { [self] in
                
                if success {
                    // 成功处理，可以跳转到其他页面
                    print("登录成功，跳转到下一个页面")
                    
                    self?.isLoginSuccessful = true
                    
                } else {
                    self?.showError = true
                    self?.errorMessage = error ?? "未知错误"
                }
            }
        }
    }
}
